package com.shinbada.modules.sys.entity;

import com.shinbada.core.excel.options.UserState;
import com.shinbada.excel.annotation.Excel;
import com.shinbada.excel.annotation.ExcelField;
import com.shinbada.modules.sys.service.UserService;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.ToString;

import java.util.Collections;

/**
 * @author initcap
 * @date Created in 2021/3/31 09:33.
 */
@Data
@ToString
@Excel("用户导入")
@ApiModel("用户导入信息实体")
public class UserImport {

    private String id;

    @ApiModelProperty("归属部门")
    @ExcelField(value = "部门", name = "office.levelName", width = 360, required = true)
    private Office office;

    @ExcelField(value = "登录名", width = 120, required = true)
    @ApiModelProperty("登录名")
    private String loginName;

    @ExcelField(value = "姓名", width = 120, required = true)
    @ApiModelProperty("姓名")
    private String name;

    @ApiModelProperty("密码")
    @ExcelField(value = "密码", width = 120, required = true)
    private String password;

    @ApiModelProperty("角色")
    @ExcelField(value = "角色", name = "role.name", width = 120, required = true)
    private Role role;

    @ExcelField(
            value = "状态",
            readConverterExp = "禁用=0,启用=1",
            writeConverterExp = "0=禁用,1=启用",
            options = UserState.class,
            required = true
    )
    @ApiModelProperty("是否允许登陆")
    private String loginFlag;

    public User toUser() {
        User user = new User();
        user.setName(this.name);
        user.setLoginFlag(this.loginFlag);
        user.setLoginName(this.loginName);
        user.setOffice(this.office);
        user.setPassword(UserService.entryptPassword(this.password));
        user.setRoleList(Collections.singletonList(this.role));
        return user;
    }

}
